const nodemailer = require('nodemailer')
const config = require('../config');
const { E_MAIL, E_PASS, CODEVALIDTIME } = config.email;

const emailOptions = {
    service: 'qq', // 使用了内置传输发送邮件 查看支持列表：https://nodemailer.com/smtp/well-known/
    port: 465, // SMTP 端口
    secureConnection: true, // 使用了 SSL
    auth: {
        user: E_MAIL,
        pass: E_PASS,  //这个是开启`POP3/SMTP/IMAP`的授权码
    }
};
const transporter = nodemailer.createTransport(emailOptions);

const curring = function (option) {
    return function (op) {
        let mailOptions = {
            ...option,
            ...op,
        }
        return transporter.sendMail(mailOptions)
    }
}
const sendMail = curring({
    from: E_MAIL,
    cc: E_MAIL,
})
const sendRegistrationEmail = function (to, code, info) {
    let { href, date } = info;
    if (!to || !code) {
        return Promise.reject({ message: '邮箱和验证码不能为空' })
    }
    sendMail({
        to: to, // 目标邮箱
        subject: `电子邮件验证码：${code}`,
        text: '用户注册',
        html: `
        <!DOCTYPE html>
        <html lang="zh-cn">
        
        <head>
            <meta charset="UTF-8">
            <meta http-equiv="X-UA-Compatible" content="IE=edge">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <title>邮箱验证</title>
        </head>
        
        <body>
            <div align="center"
                style="border-style: solid;border-width: thin;border-color: #dadce0;border-radius: 8px;padding: 40px 20px;max-width: 500px;margin: 0 auto;">
                <img src="${href + '/logo192.png'}" width="74" height="74" aria-hidden="true" style="margin-bottom: 16px;">
                <div style="font-size: 14px;color: rgba(0, 0, 0, 0.87);text-align: left;">
                    <div title" style="margin-bottom: 22px;font-size: 24px;text-align: center;">用户注册邮箱验证 </div>
                    <!-- <div title" style="margin-bottom: 22px;font-size: 14px;text-align: center;">请验证您的邮箱 </div> -->
                    <div style="margin-bottom: 16px;">
                        尊敬的用户，<a href="${href}" style="color: #000;">幽钥的小站</a>收到了您使用<b>${to}</b>邮箱用于在本站注册为用户的请求。
                    </div>
                    <div style="margin-bottom: 16px;">请使用此验证码，完成邮箱验证：</div>
                    <div style="margin-bottom: 16px;text-align: center;font-size: 36px;line-height: 44px;color: #9CCC65;">${code}</div>
                    <div style="margin-bottom: 16px;">此验证码将在 <b style="color: #FF9800;">${CODEVALIDTIME / 60}分钟</b> 后失效。</div>
                    <div style="margin-bottom: 16px;">如果您没有做过此操作，可以放心地忽略这封电子邮件。</div>
                    <div style="margin-bottom: 16px;color: #00BCD4;"><a href="${href}" style="color: inherit;">幽钥的小站</a></div>
                    <div style="margin-bottom: 16px;color: #00BCD4;">发送时间：${date}</div>
                </div>
            </div>
        </body>
        
        </html>
        `
    })
}

exports.transporter = transporter;
exports.sendMail = sendMail;
exports.sendRegistrationEmail = sendRegistrationEmail;